<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xinlizhang
// +----------------------------------------------------------------------
namespace api\portal\service;

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode;
use Endroid\QrCode\Writer\PngWriter;

class PromoteService
{
    // 检查并返回二维码路径
    public function checkQrCode($urlPromote, $username, $avatarUrl)
    {
        $file_path = CMF_DATA . '/' . 'qrcode/';
        if (!file_exists($file_path)) {
            mkdir($file_path, 0777, true);
        }
        $qrCodeFilePath = $file_path . md5($urlPromote) . '.png';

        // 判断是否已经生成过二维码 如果生成过直接用
        if (!file_exists($qrCodeFilePath)) {
            // 创建二维码对象
            $qrCode = QrCode::create($urlPromote)
                ->setEncoding(new Encoding('UTF-8'))
                ->setErrorCorrectionLevel(ErrorCorrectionLevel::Low)
                ->setSize(300)
                ->setMargin(10)
                ->setRoundBlockSizeMode(RoundBlockSizeMode::Margin)
                ->setForegroundColor(new Color(0, 0, 0))
                ->setBackgroundColor(new Color(255, 255, 255));
            $label = Label::create('')->setTextColor(new Color(255, 255, 255));
            $writer = new PngWriter();
            $result = $writer->write($qrCode, null, $label);
            $result->saveToFile($qrCodeFilePath);
        }

        $qrcodeImg = imagecreatefrompng($qrCodeFilePath);

        // 背景图片 设置里有从设置拿 没有用默认的
        $promote = cmf_get_option('promote');
        $promoteQrcodeBg = isset($promote['promote_qrcode_bg']) ? 
            cmf_get_image_url($promote['promote_qrcode_bg']) : WEB_ROOT . '/static/images/default-promote-bg.png';

        // 用户名
        $backImg = imagecreatefromstring(file_get_contents($promoteQrcodeBg));
        imagettftext($backImg, 20, 0, 330, 50, imagecolorallocate($backImg, 0, 0, 0), WEB_ROOT . '/static/fonts/simsun.ttc', '用户[' . $username . ']');
        imagettftext($backImg, 20, 0, 330, 100, imagecolorallocate($backImg, 0, 0, 0), WEB_ROOT . '/static/fonts/simsun.ttc', '邀请您加入');
        // // 添加头像
        // $avatarImg = imagecreatefromstring(file_get_contents($avatarUrl));
        // imagecopymerge($backImg, $avatarImg, 100, 200, 0, 0, imagesx($avatarImg), imagesy($avatarImg), 100);

        //合成图片
        $qrCodeBgFilePath = $file_path . md5($urlPromote) . '_bg.png';
        imagecopymerge($backImg, $qrcodeImg, 0, 0, 0, 0, imagesx($qrcodeImg), imagesy($qrcodeImg), 100);
        imagepng($backImg, $qrCodeBgFilePath);

        return $qrCodeBgFilePath;
    }

}
